package config.Interceptor;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.RedirectView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: session 检查
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/09/29$ 08:55$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Component
public class SessionInterceptor implements HandlerInterceptor {

    private StringRedisTemplate redisTemplate;

    public SessionInterceptor(StringRedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;

    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了拦截器");
        Cookie[] cookie = request.getCookies();
        String key  = cookie[0].getValue();
        System.out.println("session=======>"+key);
        String bool =  redisTemplate.opsForValue().get(key) !=null?
                redisTemplate.opsForValue().get(key).toString():null;
        if (null!=bool){
            HandlerMethod h = (HandlerMethod) handler;
            //填写拦截逻辑（非静态资源）
            return true;
        }
        //response.sendRedirect("/session");
        return false;
    }

    /*@Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }*/

}
